﻿using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Media;

namespace AntaresShell.Common
{
    public class BindingService
    {
        public static readonly DependencyProperty FullTextProperty = DependencyProperty.RegisterAttached(
            "FullText",
            typeof(string),
            typeof(BindingService),
            new PropertyMetadata(null, OnTextChangedCallback)
            );

        public static readonly DependencyProperty HighlightedTextProperty = DependencyProperty.RegisterAttached(
            "HighlightedText",
            typeof(string),
            typeof(BindingService),
            new PropertyMetadata(null, OnTextChangedCallback)
            );

        public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.RegisterAttached(
            "HighlightBrush",
            typeof(Brush),
            typeof(BindingService),
            new PropertyMetadata(null, OnHighlightBrushChangedCallback)
    );

        private static void OnHighlightBrushChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBlock textBlock = d as TextBlock;
            if (textBlock == null)
            {
                return;
            }

            Brush brush = GetHighlightBrush(textBlock);

            for (int i = 0; i < textBlock.Inlines.Count; i += 2)
            {
                textBlock.Inlines[i].Foreground = brush;
            }
        }

        private static void OnTextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBlock textBlock = d as TextBlock;
            if (textBlock == null)
            {
                return;
            }
            string fullText = GetFullText(textBlock);
            string highlightedText = GetHighlightedText(textBlock);

            if (string.IsNullOrEmpty(fullText) || highlightedText == null)
            {
                return;
            }

            var words = highlightedText.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            Brush brush = GetHighlightBrush(textBlock) ?? new SolidColorBrush(Windows.UI.Colors.Red);

            //highlightedText = highlightedText.Substring(1, highlightedText.Length - 2);

            int length = 0;
            int lastIndex = 0;
            int index = -1;

            foreach (var word in words)
            {
                var t = fullText.ToLower().IndexOf(word.ToLower(), 0, StringComparison.CurrentCultureIgnoreCase);
                if (index == -1 || t < index)
                {
                    index = t;
                    length = word.Length;
                }
            }

            textBlock.Inlines.Clear();
            while (index >= 0)
            {
                textBlock.Inlines.Add(new Run { Text = fullText.Substring(lastIndex, index - lastIndex) });
                textBlock.Inlines.Add(new Run { Text = fullText.Substring(index, length), Foreground = brush });
                lastIndex = index + length;
                index = -1;
                foreach (var word in words)
                {
                    var t = fullText.ToLower().IndexOf(word.ToLower(), lastIndex, StringComparison.CurrentCultureIgnoreCase);
                    if (index == -1 || t < index)
                    {
                        index = t;
                        length = word.Length;
                    }
                }
            }
            textBlock.Inlines.Add(new Run { Text = fullText.Substring(lastIndex) });
        }

        public static void SetHighlightedText(TextBlock element, string value)
        {
            element.SetValue(HighlightedTextProperty, value);
        }
        public static string GetHighlightedText(TextBlock element)
        {
            return (string)element.GetValue(HighlightedTextProperty);
        }

        public static void SetFullText(TextBlock element, string value)
        {
            element.SetValue(FullTextProperty, value);
        }
        public static string GetFullText(TextBlock element)
        {
            return (string)element.GetValue(FullTextProperty);
        }

        public static void SetHighlightBrush(TextBlock element, Brush value)
        {
            element.SetValue(HighlightBrushProperty, value);
        }
        public static Brush GetHighlightBrush(TextBlock element)
        {
            return (Brush)element.GetValue(HighlightBrushProperty);
        }
    }
}